package com.freesky.biz.icbc.service.impl;


import com.freesky.biz.icbc.domain.TFinanceAccount;
import com.freesky.biz.icbc.domain.TFinanceReferrer;
import com.freesky.biz.icbc.domain.TFinanceReferrerCoupon;
import com.freesky.biz.icbc.mapper.FinanceReferrerMapper;
import com.freesky.biz.icbc.service.IFinanceReferrerService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FinanceReferrerServiceImpl implements IFinanceReferrerService {

    @Autowired
    private FinanceReferrerMapper financeReferrerMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TFinanceReferrer> findList(TFinanceReferrer financeReferrer) {
        return financeReferrerMapper.findList(financeReferrer);
    }

    @Override
    public AjaxResult exportFile(TFinanceReferrer financeReferrer) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("FINANCE_REFERRER");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(financeReferrer, new TFinanceReferrer(), IFinanceReferrerService.class,
                "findList", "推荐人",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public List<TFinanceReferrerCoupon> findReferrerCouponList(TFinanceReferrerCoupon referrerCoupon) {
        return financeReferrerMapper.findReferrerCouponList(referrerCoupon);
    }

    @Override
    public AjaxResult exportCouponFile(TFinanceReferrerCoupon referrerCoupon) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("FINANCE_REFERRER_COUPON");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(referrerCoupon, new TFinanceReferrerCoupon(), IFinanceReferrerService.class,
                "findReferrerCouponList", "领取记录",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }

    @Override
    public List<TFinanceAccount> findAccountList(TFinanceAccount financeAccount) {
        return financeReferrerMapper.findAccountList(financeAccount);
    }

    @Override
    public AjaxResult exportAccountFile(TFinanceAccount financeAccount) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("FINANCE_REFERRER_ACCOUNT");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(financeAccount, new TFinanceAccount(), IFinanceReferrerService.class,
                "findAccountList", "推荐记录",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }
}
